<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片项 -->
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="8">
          <!-- 搜索框及添加按钮 -->
          <el-input placeholder="请输入内容" clearable>
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary">添加商品</el-button>
        </el-col>
      </el-row>
      <el-table :data="orderList" style="width: 100%">
        <el-table-column prop="order_number" label="订单编号">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格"> </el-table-column>
        <el-table-column prop="is_send" label="是否付款">
          <template slot-scope="scope">
            <div>
              <el-tag type="success" v-if="scope.row.is_send == '是'"
                >已付款</el-tag
              >
              <el-tag type="danger" v-else>未付款</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="下单时间">
          <template slot-scope="scope">
            <div>
              {{ scope.row.create_time | dateformate }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template>
            <div>
              <el-button
                size="mini"
                type="primary"
                icon="el-icon-edit"
                @click="openEditDialog"
              ></el-button>
              <el-button
                size="mini"
                type="success"
                icon="el-icon-location"
                @click="openProgressDialog"
              ></el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryParams.pagenum - 0"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryParams.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 修改订单信息弹出框 -->
    <el-dialog
      title="修改订单信息"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClose"
    >
      <!-- 表单区域 -->
      <el-form
        :model="orderEditForm"
        :rules="orderEditFormRules"
        ref="orderEditFormRef"
        label-width="100px"
      >
        <!-- 省市区联动区域 -->
        <el-form-item label="省市区/县" prop="area">
          <el-cascader
            v-model="orderEditForm.area"
            :options="arealist"
            :props="{ expandTrigger: 'hover' }"
            width
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="orderEditForm.address"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 物流进度对话框 -->
    <el-dialog
      title="修改订单信息"
      :visible.sync="progressDialogVisible"
      width="50%"
    >
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import arealist from "./citydata.js";
export default {
  data() {
    return {
      queryParams: {
        query: "",
        pagenum: 1,
        pagesize: 5,
      },
      total: 0,
      orderList: [],
      //修改订单信息对话框显示标志位
      editDialogVisible: false,
      //订单修改表单
      orderEditForm: {
        area: [],
        address: "",
      },
      //订单修改表单验证规则
      orderEditFormRules: {
        area: [{ required: true, message: "请选择地区", trigger: "blur" }],
        address: [
          { required: true, message: "请填写详细地址", trigger: "blur" },
        ],
      },
      //地区列表
      arealist,
      //物流信息对话框显示标志位
      progressDialogVisible: false,
      //物流信息
      progressInfo: []
    };
  },
  created() {
    this.queryOrderList();
  },
  methods: {
    async queryOrderList() {
      const { data: res } = await this.$http.get("/orders", {
        params: this.queryParams,
      });
      if (res.meta.status != 200)
        return this.$message.error("订单列表获取失败");
      this.orderList = res.data.goods;
      this.total = res.data.total;
      this.queryParams.pagenum = res.data.pagenum;
      console.log(this.orderList);
    },
    //监听页容量变化
    handleSizeChange(val) {
      this.queryParams.pagesize = val;
      this.queryOrderList();
    },
    //监听页码变化
    handleCurrentChange(val) {
      this.queryParams.pagenum = val;
      this.queryOrderList();
    },
    //监听修改订单按钮点击事件
    openEditDialog() {
      this.editDialogVisible = true;
    },
    //监听修改订单对话框关闭事件
    editDialogClose() {
      this.orderEditForm.area = [];
    },
    //监听物流信息按钮点击事件
    async openProgressDialog(){
        this.progressDialogVisible = true
        //此接口调用会导致后台崩溃
        // const {data: res} = await this.$http.get('/kuaidi/804909574412544580')
        // if(res.meta.status != 200) return this.$message.error('物流信息拉取失败')
        // this.progressInfo = res.data
    }
  },
};
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
